package com.yqg.education.service.impl;

import com.yqg.education.entity.ReceiveAnnounce;
import com.yqg.education.mapper.ReceiveAnnounceMapper;
import com.yqg.education.service.IReceiveAnnounceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ReceiveAnnounceServiceImpl 服务实现类
 * @author 张芸博
 * @since 2025-04-10
 */
@Service
public class ReceiveAnnounceServiceImpl extends ServiceImpl<ReceiveAnnounceMapper, ReceiveAnnounce> implements IReceiveAnnounceService {
    @Resource
    private ReceiveAnnounceMapper receiveAnnounceMapper;

    @Override
    public Map<String, Object> findReceiveAnnouncePage(Integer pageNum, Integer pageSize, String name, Integer userId) {
        Integer pageStartNumber = pageNum * pageSize - pageSize;

        List<Map<String,Object>> receiveAnnounceList = receiveAnnounceMapper.stuFindReceiveAnnouncePage(pageStartNumber, pageSize, name,userId);
        receiveAnnounceList.forEach(receiveAnnounce -> {
            // 时间格式调整
            LocalDateTime sendTime = (LocalDateTime) receiveAnnounce.get("sendTime");
            receiveAnnounce.put("sendTime", sendTime.atZone(ZoneId.systemDefault())
                    .withZoneSameInstant(ZoneId.of("Asia/Shanghai"))
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        });
        Long count = receiveAnnounceMapper.stuFindReceiveAnnouncePageCount(name,userId);
        Map<String, Object> map = new HashMap<>();
        map.put("records", receiveAnnounceList);
        map.put("total", count);
        return map;
    }
}
